package com.dstation.service;

import com.dstation.dao.UserInfoMapper;
import com.dstation.domain.UserInfo;
import com.dstation.service.interf.IUserService;
import com.dstation.utils.Constants;
import com.dstation.utils.MD5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public boolean findUserPhoneIsExistByPhone(String userPhone) {
        String result = userInfoMapper.selectUserPhoneIsExistByPhone(userPhone);
        return (result == null) ? false : true;
    }

    @Override
    public boolean addUserInfo(UserInfo userInfo) {
        //初始化默认的用户信息
        userInfo.setUserGender(Constants.USER_DEFAULT_GENDER);
        userInfo.setUserBirthday(new Timestamp(System.currentTimeMillis()));
        userInfo.setUserSignature(Constants.USER_DEFAULT_SIGNATURE);
        userInfo.setUserBalance(Constants.USER_DEFAULT_BALANCE);
        userInfo.setPrivilegetimes(Constants.USER_DEFAULT_PRIVILEGETIMES);
        userInfo.setUserIcon(Constants.USER_DEFAULT_ICON);
        userInfo.setCreateDate(new Timestamp(System.currentTimeMillis()));
        try {
            userInfo.setUserPassword(MD5.encryption(userInfo.getUserPassword(), Constants.KEY));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return userInfoMapper.insertUserInfo(userInfo);
    }

    @Override
    public UserInfo findUserInfo(UserInfo userInfo) {
        try {
            userInfo.setUserPassword(MD5.encryption(userInfo.getUserPassword(), Constants.KEY));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return userInfoMapper.selectUserInfo(userInfo);
    }

    @Override
    public UserInfo findUserInfoByUserId(Long userId) {
        return userInfoMapper.findUserInfoByUserId(userId);
    }

    @Override
    public boolean alterUserPrivilegetimesByUserId(Long userId) {
        return userInfoMapper.updateUserPrivilegetimesByUserId(userId);
    }

    @Override
    public Boolean alterUserBlanceByUserId(long userId,int operate) {
        return userInfoMapper.updateUserBlanceByUserId(userId,operate);
    }

    @Override
    public int findFansNumByUserId(Long userId) {
        return userInfoMapper.selectFansNumByUserId(userId);
    }

    @Override
    public int alterPrivilegetimesByUserID(Long userId, Integer privilegetimes) {
        return userInfoMapper.updatePrivilegetimesByUserID(userId,privilegetimes);
    }

    @Override
    public Boolean alterUserInfo(UserInfo userInfo) {
        return userInfoMapper.updateUserInfo(userInfo);
    }
}
